﻿using System;
using System.Data;
using AntdUI;
using System.Windows.Forms;
using CardRegisterCollection.Model;
using _9jCardRegisterCollection.Models;
using Mapster;
using System.Drawing;

namespace _9jCardRegisterCollection.Views
{
    public partial class UserManagementPage : UserControl
    {
        private Window window;
        public UserManagementPage(Window window)
        {
            this.window = window;

            InitializeComponent();

            //初始化表格列头
            InitTableColumns();
            InitData();
        }



        private readonly CardRegisterCollection.BLL.User _userBll = new CardRegisterCollection.BLL.User();


        private AntList<UserExpand> _dataList;

        /// <summary>
        /// 初始化表格列头
        /// </summary>
        private void InitTableColumns()
        {
            var columns = new ColumnCollection()
            {
                new Column("UserName", "登录账号", ColumnAlign.Center),
                new Column("TrueName", "姓名", ColumnAlign.Center),
                new Column("UnitName", "单位名称", ColumnAlign.Center),
                new Column("DeptName", "部门名称", ColumnAlign.Center),
                new Column("CreatedBy", "创建账号", ColumnAlign.Center),
                new Column("CreatedAt", "创建时间", ColumnAlign.Center),
                new Column("Links", "操作", ColumnAlign.Center)
                {
                    Fixed = true,
                },
            };
            tabUsers.Columns = columns;
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        private void InitData()
        {
            _dataList = new AntList<UserExpand>();
            var data = _userBll.GetList("");
            if (data != null)
            {
                foreach (UserExtension model in data)
                {
                    var userExpand = model.Adapt<UserExpand>();
                    userExpand.Links = new CellLink[]
                    {
                 new CellButton("Edit","编辑",TTypeMini.Primary),
                 new CellButton("Del","删除",TTypeMini.Error),
                 new CellButton("Auth","授权",TTypeMini.Primary),
                    };
                    _dataList.Add(userExpand);
                }
            }
            tabUsers.Binding(_dataList);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butSearch_Click(object sender, System.EventArgs e)
        {
            InitData();
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butCreate_Click(object sender, System.EventArgs e)
        {
            var user = new User()
            {
                ID = Guid.NewGuid().ToString(),
                CreatedAt = DateTime.Now,
                CreatedBy = CurrentLoginInfo.UserInfo.UserName
            };
            var form = new UserForm(window, user);
            Modal.open(new Modal.Config(window, "账号新增", form)
            {
                BtnHeight = 0,
            });
            if (form.Submit)
            {
                _userBll.Add(user);
                InitData();
            }
        }


        /// <summary>
        /// 操作按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabUsers_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var data = (UserExpand)e.Record;
            if (data == null) return;

            var id = e.Btn.Id;
            switch (id)
            {
                case "Edit":
                    {
                        var form = new UserForm(window, data);
                        Modal.open(new Modal.Config(window, "修改账号信息", form)
                        {
                            BtnHeight = 0,
                        });
                        if (form.Submit)
                        {
                            _userBll.Update(data);
                            InitData();
                        }
                    }
                    break;
                case "Del":
                    {
                        var rlt =Modal.open(new Modal.Config(window, "系统消息", "确定要删除吗？")
                        {
                            Icon = TType.Info,
                            Padding = new Size(24, 20),
                        });
                        if (rlt == DialogResult.OK)
                        {
                            _userBll.Delete(data.ID);
                            InitData();
                        }
                    }
                    break;
                case "Auth":
                    {
                        var form = new UserRoleAuth(window, data.ID);
                        Modal.open(new Modal.Config(window, "账号授权", form)
                        {
                            BtnHeight = 0,
                        });
                    }
                    break;
            }
        }
    }
}
